package tv.huan.web.controller.media;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.common.utils.poi.ExcelUtil;
import tv.huan.media.domain.Item;
import tv.huan.media.service.IItemService;

/**
 * 项目Controller
 * 
 * @author admin
 * @date 2019-10-08
 */
@Controller
@RequestMapping("/media/item")
public class ItemController extends BaseController
{
    private String prefix = "media/item";

    @Autowired
    private IItemService itemService;

    @RequiresPermissions("media:item:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("media:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Item item)
    {
        startPage();
        List<Item> list = itemService.selectItemList(item);
        return getDataTable(list);
    }

    /**
     * 查询未推荐的项目
     */
    @RequiresPermissions("media:item:list")
    @PostMapping("/noRecommendList/{albumIds}")
    @ResponseBody
    public TableDataInfo noRecommendList(Item item, @PathVariable("albumIds") String albumIds)
    {
        startPage();
        List<Item> list = itemService.selectNoRecommendList(albumIds,item.getName());
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @RequiresPermissions("media:item:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Item item)
    {
        List<Item> list = itemService.selectItemList(item);
        ExcelUtil<Item> util = new ExcelUtil<Item>(Item.class);
        return util.exportExcel(list, "item");
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目
     */
    @RequiresPermissions("media:item:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Item item)
    {
        return toAjax(itemService.insertItem(item));
    }

    /**
     * 修改项目
     */
    @GetMapping("/edit/{itemId}")
    public String edit(@PathVariable("itemId") Long itemId, ModelMap mmap)
    {
        Item item = itemService.selectItemById(itemId);
        mmap.put("item", item);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("media:item:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Item item)
    {
        return toAjax(itemService.updateItem(item));
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("media:item:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try {
            itemService.deleteItemByIds(ids);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
